<?php
namespace app\api\controller\xilujob;


use app\common\controller\xilujob\XilujobApi;
use app\common\model\xilujob\CompanyProfile;
use app\common\model\xilujob\Config;
use app\common\model\xilujob\Package AS PackageModel;
use app\common\model\xilujob\PackageOrder;
use app\common\model\xilujob\Third;
use think\Db;
use think\Exception;

class Package extends XilujobApi
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];
    protected $restrictedActions = ['*'];

    /**
     * 套餐列表
     * @return \think\response\Json
     */
    public function lists(){
        $pagesize = $this->request->param('pagesize',10);
        $lists = PackageModel::where('status',1)
            ->field("id,name,salesprice,expires,unit,job_publish_num,resume_view_num,interview_num,job_refresh_num")
            ->paginate($pagesize);
        $this->success("查询成功",$lists);

    }

    public function order(){
        $params = $this->request->post('');
        if(!$params){
            $this->error("参数错误");
        }
        $params['platform'] = 'wxmini';
        $company = CompanyProfile::where('user_id',$this->auth->id)->find();
        if($params['pay_type'] == 1){
            $third = Third::where('id',$params['third_id'])->find();
            if(!$third){
                $this->error('未找到登录信息');
            }
            $params['openid'] = $third->openid;
            unset($params['third_id']);
        }
        Db::startTrans();
        try {
            $ret = PackageOrder::createOrder($params,$company->id,$this->auth->id);
        }catch (Exception $e){
            Db::rollback();
            $this->error($e->getMessage());
        }
        Db::commit();
        $this->success('订单创建成功',$ret);
    }

}